<template>
	<view class="add-comment-container">
		<view class="text-box">
			<view class="label">
				填写评价
			</view>
			<textarea v-model="comment.content"></textarea>
		</view>
		<view class="btn-submit">
			<view class="submit" @click="submit">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
	} from 'vuex'
	export default {
		data() {
			return {
				comment: {
					goodsId: '',
					content: '',
					status: 1,
				}
			};
		},
		onLoad(e) {
			this.comment.goodsId = e.goodsId
		},
		computed: {
			...mapState('module_user', ['userinfo', 'token'])
		},
		methods: {
			...mapMutations('module_user', ['updateRedirectInfo']),
			async submit() {
				if (!this.token || this.token === '') {
					uni.$showMsg('请先登录')
					this.updateRedirectInfo({
						openType: 'switchPage',
						from: `/subcontract/add_comment/add_comment?goodsId=${this.comment.goodsId}`
					})
					return uni.navigateTo({
						url: '/subcontract/sign_in/sign_in'
					})
				}
				if(!this.comment.content && this.comment.content === '') return uni.$showMsg('请输入内容')
				const { data: res } = await uni.$http.post('/user/comments', this.comment)
				console.log(res);
				if(res.code === 200) {
					let pages = getCurrentPages()
					let beforePage = pages[pages.length - 2]; // 上一页
					// beforePage.$vm.data
					uni.navigateBack({
					    success: () => {
					        beforePage.getData(this.comment.goodsId);
					    }
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.add-comment-container {
		padding: 10px;
	}
	.text-box {
		margin-top: 20rpx;

		.label {
			font-weight: bold;
			padding: 10px;
		}
	}

	textarea {
		width: 100%;
		height: 100px;
		margin-top: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		border: 1px solid #f0f0f0;
	}

	.btn-submit {
		padding: 30rpx;

		.submit {
			width: 100%;
			color: #fff;
			height: 40px;
			line-height: 40px;
			text-align: center;
			background-color: aqua;
		}
	}
</style>
